home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2BGI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  8.5 KB  |  269 lines

  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7.  
  8. /*
  9. ╔═════════════════════════════════════════════════════════════════╗
  10. ║             █▓▒░ WordUp Graphics Toolkit V4.0 ░▒▓█              ║
  11. ║ Conversion Utility   Copyright 1994 WordUp Software Productions ║
  12. ╟─────────────────────────────────────────────────────────────────╢
  13. ║ Program:      spr2bgi.c                                         ║
  14. ║ Description:  Utility for converting a WGT sprite file to       ║
  15. ║               a BGI file format.                                ║
  16. ║                                                                 ║
  17. ║ Written by:   Chris Egerter                                     ║
  18. ╚═════════════════════════════════════════════════════════════════╝
  19. */
  20.  
  21.  
  22. /*
  23. ╔═════════════════════════════════════════════════════════════════╗
  24. ║ Generic sprite conversion utility                               ║
  25. ║ This version uses the BGI to load a sprite file, and convert it ║
  26. ║ into 640x350x16 mode.  Note that you cannot create sprites with ║
  27. ║ more than 16 colors and expect it to look right after converting║
  28. ║ it.  To do this correctly, draw using the first 16 colors only. ║
  29. ║ If you modify the palette, you will need to change it yourself  ║
  30. ║ using the first 16 colors values in the palette variable.       ║
  31. ║                                                                 ║
  32. ║ This program can be used as a generic conversion program.       ║
  33. ║ Simply replace the BGI graphics commands with any similar       ║
  34. ║ commands from another graphics library.                         ║
  35. ║                                                                 ║
  36. ║ The main commands you will need to change are:                  ║
  37. ║    initialization routines                                      ║
  38. ║    putpixel                                                     ║
  39. ║    imagesize (finds the number of bytes required in an image)   ║
  40. ║    farmalloc   (WGT calculates the size and allocates the memory║
  41. ║                 for you, other libraries may not.  You will     ║
  42. ║                 have to be flexible here.)                      ║
  43. ║    getimage                                                     ║
  44. ║    putimage                                                     ║
  45. ║    getpalette                                                   ║
  46. ║    setrgbpalette                                                ║
  47. ║    cleardevice                                                  ║
  48. ║    bar                                                          ║
  49. ╚═════════════════════════════════════════════════════════════════╝
  50. */
  51.  
  52. void far *sprites[2001];
  53. void convertsprites (char *infile, char *outfile);
  54. void loadsprites (char *infile, void far *loadspr[]);
  55. void testsprites ();
  56. void freesprites (void far *freespr[]);
  57.  
  58.  
  59. int main(void)
  60. {
  61.    /* request auto detection */
  62.    int gdriver = VGA, gmode = VGAMED, errorcode;
  63.  
  64.    /* initialize graphics mode */
  65.    initgraph(&gdriver, &gmode, "..\\BGI");  /* Change this as necessary */
  66.  
  67.    /* read result of initialization */
  68.    errorcode = graphresult();
  69.  
  70.    if (errorcode != grOk)  /* an error occurred */
  71.    {
  72.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  73.       printf("Press any key to halt:");
  74.       getch();
  75.       exit(1);             /* return with error code */
  76.    }
  77.  
  78.    convertsprites ("sprtconv.spr", "out.spr");
  79.    loadsprites ("out.spr", sprites);
  80.    testsprites ();
  81.    freesprites (sprites);
  82.  
  83.    /* clean up */
  84.    closegraph ();
  85.    return 0;
  86. }
  87.  
  88. void convertsprites (char *infile, char *outfile)
  89. {
  90. FILE *in;                               /* 256 color sprite file */
  91. FILE *out;                              /* converted sprite file */
  92. unsigned char palette[768];         /* 256 * 3 (RGB values) */
  93. int maxcolor;
  94. int maxsprite;
  95. unsigned size;
  96.  
  97. void far *temp;
  98. int a, b, i, spritemade;
  99. char buf[14];
  100. int x, y;
  101. int startingsprite;
  102.  
  103. /* Open the files */
  104.  
  105.  if ((in = fopen (infile, "rb")) == NULL)
  106.   {
  107.    closegraph ();
  108.    printf ("Could not open 256 color sprite file");
  109.    exit (1);
  110.   }
  111.  if ((out = fopen (outfile, "wb")) == NULL)
  112.   {
  113.    closegraph ();
  114.    printf ("Could not open converted sprite file");
  115.    exit (1);
  116.   }
  117.  
  118.  fread(&a, 2, 1, in);
  119.     /* Get the version number, and change the startingsprite accordingly.
  120.      If version <= 3, maxsprite contains the maximum number of sprites
  121.      that can be stored in a file.  If version > 4, maxsprite contains
  122.      the number of the highest sprite in the file. (empty sprites at
  123.      the end are not kept in the file. */
  124.  if (a <= 3)
  125.    startingsprite = 1;
  126.  else startingsprite = 0;    /* Version 4 starts at sprite 0 */
  127.                  
  128.  
  129.  fread (buf, 1, 13, in); /* sprite header */
  130.  if (0 == strnicmp (" Sprite File ", buf, 13)) /* see if it is a sprite file */
  131.    {
  132.     fread (palette, 1, 768, in); /* Read in 256 color palette */
  133.     maxcolor = getmaxcolor ();
  134.     fwrite(&maxcolor, 1, 2, out); 
  135.     /* Write the number of colors stored in file */
  136.  
  137.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  138.      {
  139.       fputc (palette[i*3], out);    /* Write out Red */
  140.       fputc (palette[i*3+1], out);  /* Green */
  141.       fputc (palette[i*3+2], out);  /* And Blue color values */
  142.      }
  143.         
  144.     fread(&maxsprite, 2, 1, in); /* maximum sprites in this file */
  145.     fwrite(&maxsprite, 1, 2, out); 
  146.     for (i = startingsprite; i <= maxsprite; i++) /* load them in */
  147.      {
  148.       fread(&spritemade, 2, 1, in); /* flag to see if sprite exists */
  149.       fwrite(&spritemade, 1, 2, out); 
  150.       if (spritemade == 1)
  151.        {
  152.         setfillstyle (SOLID_FILL, 0);
  153.     bar (0, 0, 319, 199);  /* maximum sprite size */
  154.         fread(&a, 2, 1, in);   /* get width and height */
  155.         fread(&b, 2, 1, in);
  156.         fwrite(&a, 1, 2, out); /* put width and height */
  157.         fwrite(&b, 1, 2, out); 
  158.  
  159.     /* Read in the image data. Each byte represents a color
  160.     from 0-255. Obviously converting sprites that use more colors
  161.     than the current mode allows will not work. Draw sprites using
  162.     only the first colors (eg 0-16), up to maxcolors of the mode
  163.         you're using. */
  164.  
  165.     for (y = 0; y < b; y++)
  166.       for (x = 0; x < a; x++)
  167.         putpixel (x, y, fgetc (in));
  168.  
  169.         size = imagesize(0, 0, a - 1, b - 1); /* get byte size of image */
  170.     if ((temp = farmalloc (size)) == NULL)
  171.      {
  172.       closegraph ();
  173.       printf ("Error: not enough heap space in convertsprites.\n");
  174.       exit (1);
  175.      }
  176.     getimage (0, 0, a - 1, b - 1, temp); /* Get the image in new mode */
  177.     fwrite (temp, size, 1, out); /* Write the data in getimage format */
  178.     farfree (temp);
  179.        }
  180.      }
  181.    }
  182.   fclose (in);
  183.   fclose (out);
  184. }
  185.  
  186.  
  187.  
  188. void loadsprites (char *infile, void far *loadspr[])
  189. {
  190. FILE *in;   /* converted color sprite file */
  191. int maxsprite;
  192. unsigned size;
  193.  
  194. int a, b, i, spritemade;
  195. int maxcolor;
  196. struct palettetype pal;
  197.  
  198.  /* Open the files */
  199.  if ((in = fopen (infile, "rb")) == NULL)
  200.    {
  201.     closegraph ();
  202.     printf ("Could not load converted color sprite file");
  203.     exit (1);
  204.    }
  205.  
  206.  fread(&maxcolor, 2, 1, in);
  207.  
  208.  getpalette (&pal);
  209.  for (i = 0; i < maxcolor; i++) /* read in the palette */
  210.     setrgbpalette(pal.colors[i], fgetc (in), fgetc (in), fgetc (in));
  211.     
  212.  fread(&maxsprite, 2, 1, in); /* maximum sprites in this file */
  213.  
  214.  for (i = 0; i < maxsprite; i++) /* load them in */
  215.    {
  216.     fread(&spritemade, 2, 1, in); /* flag to see if sprite exists */
  217.     if (spritemade == 1)
  218.       {
  219.        fread(&a, 2, 1, in); /* get width and height */
  220.        fread(&b, 2, 1, in);
  221.        size = imagesize(0, 0, a - 1, b - 1); /* get byte size of image */
  222.        if ((loadspr[i] = farmalloc (size)) == NULL)
  223.          {
  224.           closegraph ();
  225.       printf ("Error: not enough heap space in loadsprites().\n");
  226.       freesprites (loadspr);
  227.       exit (1);
  228.      }
  229.        fread (loadspr[i], size, 1, in);
  230.       } 
  231.     else loadspr[i] = NULL;
  232.    }
  233.   fclose (in);
  234. }
  235.  
  236.  
  237.  
  238.  
  239. void testsprites (void)
  240. /* Loops through 10 sprites, displaying them on the screen
  241.    using the putimage method. Press a key to go to the next sprite. */
  242. {
  243. int i, j;
  244.  
  245. for (i = 0; i < 10; i++) 
  246.   {
  247.    cleardevice ();
  248.   
  249.    if (sprites[i] != NULL)
  250.      {
  251.       for (j = 1; j < 20; j++)
  252.         putimage (rand () % getmaxx (), rand () % getmaxy (),
  253.                   sprites[i], COPY_PUT); 
  254.        /* Put the converted image on the screen */
  255.       getch ();
  256.      }
  257.   }
  258. }
  259.  
  260. void freesprites (void far *freespr[])
  261. {
  262. int i;
  263.  
  264.  for (i = 0; i < 2001; i++)
  265.   {
  266.    if (freespr[i] != NULL)
  267.      farfree (freespr[i]);
  268.   }
  269. }